package net.pinnoocle.playshoppc.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.utils.HelpUtils;
import net.pinnoocle.playdao.entity.Group;
import net.pinnoocle.playservice.service.impl.GroupServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ye
 * @Description
 * @create 2021-12-07 14:00
 */
@Component
@Slf4j
public class GroupFailTask {

    @Autowired
    GroupServiceImpl groupService;

    /**
     * 组局自动未成局（30秒执行一次）
     */
    @Scheduled(cron="*/30 * * * * ?")
    public void groupFailTask(){
        String nowTime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm");
        //所有到时间的局
        List<Group> groupList = groupService.list(new QueryWrapper<Group>()
                .lambda()
                .le(Group::getStartTime, nowTime)
                .le(Group::getGroupType,1));
        for (Group groupItem : groupList){
            int nowNumber = groupItem.getNowNumber();
            int startNumber = groupItem.getStartNumber();
            int groupType = 0;
            //到时间了,人也满了，待开局
            if(nowNumber >= startNumber){
                groupType = 1;
            }else {
                groupType = 4;//到时间人未满，未成局
            }
            Group group = new Group();
            group.setGroupId(groupItem.getGroupId());
            group.setGroupType(groupType);
            groupService.updateById(group);
        }
    }
}
